package control
{
   import flash.display.MovieClip;
   import flash.events.Event;
   import flash.events.MouseEvent;
   import flash.text.TextField;
   import flash.text.TextFieldType;
   import flash.text.TextFormat;
   import flash.text.TextFormatAlign;

   public class CustomField extends MovieClip
   {
      private var _tf: TextField = new TextField
      private var _caption: TextField = new TextField
      private var _editable: Boolean = false
      
      public function CustomField()
      {
         super();
      }
      
      public function get caption (): String {
         if (_caption) return ""
         return _caption.text
      }

      public function set caption (value:String): void {
         trace("123")
         if (getChildByName("caption") == null) {
            _caption.x = 0
            _caption.y = 0
            _caption.height = 18
            _caption.selectable = false
            _caption.autoSize = TextFormatAlign.LEFT
            _caption.defaultTextFormat = new TextFormat("Arial", 12, 0x999999)
            addChild(_caption)
         }

         if (value == "") {
            removeChild(_caption)
            _caption = null
         } else {
            _caption.text = value
         }
         _tf.x = _caption.width + 2 
      }
      
      public function get textField (): String {
         return _tf.text
      }

      public function set textField (value:String): void {
         if (getChildByName("textField") == null) {
            _tf.x = 20
            _tf.y = 0
            _tf.name = "textField"
            _tf.width = 60
            _tf.height = 18
            _tf.defaultTextFormat = new TextFormat("Arial", 12, 0xFFFFFF)
            _tf.addEventListener(Event.CHANGE, onChaneText)
            addChild(_tf)
         }
         _tf.text = value
      }
      
      private function onChaneText (event:Event): void {
         dispatchEvent(event)
      }

      public function set editable (value:Boolean): void {
         if (_editable == value) return
         _editable = value
         if (value) {
            _tf.type = TextFieldType.INPUT
            _tf.border = true
            _tf.borderColor = 0xFFFFFF
         } else {
            _tf.type = TextFieldType.DYNAMIC
            _tf.border = false
            _tf.borderColor = 0xFFFFFF
         }
      }

      public function get editable (): Boolean {
         return _editable
      }

      
   }
}